package com.example.interview.no0201.solution;

import com.example.interview.no0201.ListNode;
import com.example.interview.no0201.Solution;

import java.util.HashSet;
import java.util.Set;

/**
 * @Description 解答接口-移除重复节点
 * @ClassName SolutionImpl
 * @Author Administrator
 * @Date 2020/7/9 14:21
 * @Version 1.0.0
 */
public class SetSolutionImpl implements Solution {
    @Override
    public ListNode removeDuplicateNodes(ListNode head) {
        Set<Integer> set = new HashSet<>();
        ListNode cur = head;
        while (cur != null && cur.next != null) {
            set.add(cur.val);
            if (set.contains(cur.next.val)) {
                cur.next = cur.next.next;
            } else {
                cur = cur.next;
            }
        }
        return head;
    }
}
